package com.tf_cut.admin.seller.controller;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tf_cut.admin.backend.api.BackEndApi;


@RestController
@RequestMapping("/sellerpayment/")
public class SellerPaymentController {
	
    @Autowired
    private BackEndApi sellerInfoApi;
	
	@ResponseBody
    @PostMapping(value = "/notify", produces = MediaType.APPLICATION_XML_VALUE)
	 public String sellerpaymentNotify(HttpServletRequest httpServletRequest) throws Exception {
		String xmlContext = getXmlContextString(httpServletRequest);
		return sellerInfoApi.sellerpaymentNotify(xmlContext);
	}
	
    public String getXmlContextString(HttpServletRequest httpServletRequest) {
        StringWriter writer = new StringWriter();
        BufferedReader reader = null;
        try {
            reader = httpServletRequest.getReader();
            char[] chars = new char[256];
            int count = 0;
            while ((count = reader.read(chars)) > 0) {
                writer.write(chars, 0, count);
            }
        } catch (Exception e) {
            //TODO 可以抛出运行时异常
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
        return writer.toString();
    }
    
    
    
	@ResponseBody
    @PostMapping(value = "/grantNoify", produces = MediaType.APPLICATION_XML_VALUE)
	 public String grantNoify(HttpServletRequest httpServletRequest) throws Exception {
		String xmlContext = getXmlContextString(httpServletRequest);
		return sellerInfoApi.grantNoify(xmlContext);
	}

}
